# -*- coding: utf-8 -*- #
#将所有抽取的正例记为Trainall有放回的随机采样80%作为训练正例Trainp，有放回的随机采样40%作为Testp
import codecs
import random
import datetime


def random_select(category_sentence_list,select_size,out):
    list_size=len(category_sentence_list)
    random.seed(datetime.datetime.now())
    while select_size>0:
        r_val=random.randint(0,list_size-1)
        out.write(category_sentence_list[r_val]+'\n')
        select_size-=1
    
    

if __name__ == '__main__':
    category_set=set()
    with codecs.open('../data/categorySizeSorted.txt','r','utf-8') as f:
        for line in f:
            array=line.split('\t:\t')
            category_set.add(array[0])
    
    category_sentence_list=[]
    first_line_flag=True
    last_category=''
    test_p_out=codecs.open('../data/test_p1.txt','w','utf-8')
    n=0
    with codecs.open('../data/categorySentenceResultSortedCateFilter.txt','r','utf-8') as f:
        for line in f:
            array=line.split('\t:\t')
            category=array[1]
            if category in category_set:
                if first_line_flag==True:
                    category_sentence_list.append(line.strip())
                    array=line.split('\t:\t')
                    category=array[1]
                    last_category=category
                    first_line_flag=False
                else:
                    
                    
                    if last_category==category:
                        category_sentence_list.append(line.strip())
                    else:
                        #先随机生成Trainp和Testp
                        test_p_size=int(len(category_sentence_list)*0.8)
                        random_select(category_sentence_list,test_p_size,test_p_out)
                        
                        
                        
                        last_category=category
                        category_sentence_list=[]
                        category_sentence_list.append(line.strip())
    
    test_p_out.close()
    
